<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-05-14 16:40:00
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-02-27 12:08:48
 */

namespace app\weixin_manage\controller;
use app\common\controller\Backend;

class Event extends Backend {

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '事件管理', '公众号管理', 'event');

		$this->weixinAccountDao = model('WeixinAccount');
		$this->weixinEventDao = model('weixin_manage/WeixinEvent');
	}

	function index() {
		if (!$this->wid) {
			$this->adminError('微信wid有误');
		}
		$map = array(
			'wid'   => $this->wid,
		);
		$count = $this->weixinEventDao->countSearch($map);
		$Page = new \org\util\PageBootstrap($count, config('app.per_page'), $parameter);
		$pageShow = $Page->show();
		list($list) = $this->weixinEventDao->search($map, $Page->getLimit());
		$this->assign('count', $count);
		$this->assign('pageShow', $pageShow);
		$this->assign('list', $list);

		$this->assign('msgtype_config', config('moduleconfig.msgtype_config')); //消息类型
		
		return $this->fetch();
	}

	public function listorders() {
		$result = parent::listorders($this->weixinEventDao);

		if ($result !== false) {
			$this->success('排序更新成功');
		} else {
			$this->error('排序更新失败');
		}
	}

	public function add() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->weixinEventDao->getInfo($id);
		} else {
			$info = [
				'status' => 1
			];
		}
		$this->assign('id', $id);
		$this->assign('info', $info);

		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		if (strpos($data['event_text'], '{d}') !== false) {
			$data['is_regular'] = 1;
		}

		$data['link'] = ueditorContent($data['link']);

		if ($data['event_msgtype'] == 2) { //图片
			//普通上传图片
			list($uploadInfo, $isDelOldImg) = $this->comuploadFile('weixin/event', 'picurl', $info['picurl'], array(), 0);
			$uploadInfo && $data['picurl'] = $uploadInfo['saveFullPath'];
			//print_r($uploadInfo);exit;

			//将图片上传到微信后台
			if ($uploadInfo) {
				set_time_limit(0);
				$filename = $uploadInfo['savepath'].$uploadInfo['savename'];
				db('Aaaa')->insert(['space'=>'weixin_manage', 'module'=>'event', 'flag'=>'filename', 'content'=>$filename]);
				//获取微信公众号信息
				$weixinAccountInfo = $this->weixinAccountDao->getInfo($this->wid);
				//微信jssdk
				$jssdk = new \org\weixin\jssdk($weixinAccountInfo['appid'], $weixinAccountInfo['appsecret'], $weixinAccountInfo['wxnumber']);
				$signPackage = $jssdk->getSignPackage();
				//获取缓存中的access_token
				$access_token = $jssdk->getAccessTokenForOut();
				$options = array(
					'token'=>$weixinAccountInfo['token'], //填写你设定的key
					'encodingaeskey'=>$weixinAccountInfo['aeskey'], //填写加密用的EncodingAESKey
					'appid'=>$weixinAccountInfo['appid'], //填写高级调用功能的app id, 请在微信开发模式后台查询
					'appsecret'=>$weixinAccountInfo['appsecret'], //填写高级调用功能的密钥
					'partnerid'=>'', //财付通商户身份标识，支付权限专用，没有可不填
					'partnerkey'=>'', //财付通商户权限密钥Key，支付权限专用
					'paysignkey'=>'' //商户签名密钥Key，支付权限专用
				);
				$weObj = new \wechat\Wechat($options);
				//使用jssdk获取到的access_token赋值，下文要用
				if ($access_token) {
					$weObj->checkAuth('', '', $access_token);
				}
				//$result = $weObj->valid(); //验证(这里不能加验证)
				
				//上传永久素材
				$result = $weObj->uploadForeverMedia([], 'images', false, [], $filename);
				db('Aaaa')->insert(['space'=>'weixin_manage', 'module'=>'event', 'flag'=>'uploadForeverMedia', 'content'=>pw_var_export($result)]);
				if ($result !== false) {
					$data['media_id'] = $result['media_id'];
					//删除原图
					unlink($filename);
				} else {
					db('Aaaa')->insert(['space'=>'weixin_manage', 'module'=>'event', 'flag'=>'error', 'content'=>'菜单生成失败，错误代码：'.$weObj->errCode.'；错误提示：'.$weObj->errMsg]);
					$this->error('图片上传失败，错误代码：'.$weObj->errCode.'；错误提示：'.$weObj->errMsg);
				}
			}
		} else {
			//普通上传图片
			list($uploadInfo, $isDelOldImg) = $this->comuploadFile('weixin/event', 'picurl', $info['picurl']);
			$uploadInfo && $data['picurl'] = $uploadInfo['saveFullPath'];
		}

		if ($id) {
			$result = $this->weixinEventDao->baseUpdateData($id, $data);
			if ($result !== false) {
				$this->success('保存成功', url('weixin_manage/event/index', ['wid'=>$this->wid]));
			} else {
				$this->error('保存失败'.showDbError($this->weixinEventDao));
			}
		} else {
			$data['wid'] = $this->wid;
			$data['create_time'] = $this->nowtime;
			$result = $this->weixinEventDao->baseAddData($data);
			if ($result !== false) {
				$this->success('添加成功', url('weixin_manage/event/index', ['wid'=>$this->wid]));
			} else {
				$this->error('添加失败'.showDbError($this->weixinEventDao));
			}
		}
	}

	public function doDelete() {
		$id = input('param.id', '', 'intval');
		if (!$id) {
			$this->error('非法ID');
		}
		//获取信息(不解析)
		$info = $this->weixinEventDao->get($id);

		//删除对应数据库信息
		$result = $this->weixinEventDao->delInfo($id);
		
		//删除图片文件
		$info['picurl'] && $this->delFtpFile($info['picurl']);
		
		if ($result) {
			$this->success('删除成功', url('weixin_manage/event/index', ['wid'=>$this->wid]));
		} else {
			$this->error('删除失败'.showDbError($this->weixinEventDao));
		}
	}

}